package com.chapp.provider.handler;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.chapp.base.Artist;
import com.chapp.provider.DroidContentProvider;

public class ArtistHandler {

	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ DroidContentProvider.AUTHORITY + "/artist");
	public static final String ID = "_id";
	public static final String NAME = "name";
	public static final String BIO = "bio";
	public static final String IMAGE = "image";

	public static Uri insertArtist(Context context, Artist artist) {
		ContentValues valores = new ContentValues();
		valores.put(NAME, artist.getName());
		valores.put(BIO, artist.getBio());
		valores.put(IMAGE, artist.getThumbnailURL());

		Uri uri = context.getContentResolver().insert(CONTENT_URI, valores);

		Log.i(uri.getPath(), uri.getPath());
		return uri;
	}

	public static List<Artist> getAllArtists(Context context) {
		Uri allArtist = CONTENT_URI;
		Cursor cursor = context.getContentResolver().query(allArtist, null,
				null, null, null);
		List<Artist> list_artist = new ArrayList<Artist>();
		if (cursor.moveToFirst()) {
			do {
				Artist artist = new Artist();
				artist.setId(cursor.getString(cursor
						.getColumnIndex(ArtistHandler.ID)));
				artist.setBio(cursor.getString(cursor
						.getColumnIndex(ArtistHandler.BIO)));
				artist.setName(cursor.getString(cursor
						.getColumnIndex(ArtistHandler.NAME)));
				artist.setThumbnailURL(cursor.getString(cursor
						.getColumnIndex(ArtistHandler.IMAGE)));
				list_artist.add(artist);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return list_artist;
	}

	public static Artist getArtist(Context context, int id) {
		Uri uri = Uri.withAppendedPath(CONTENT_URI, "" + id);
		Cursor cursor = context.getContentResolver().query(uri, null, null,
				null, null);
		Artist artist = new Artist();
		if (cursor.moveToNext()){
			artist.setName(cursor.getString(cursor.getColumnIndex(ArtistHandler.NAME)));
			artist.setBio(cursor.getString(cursor.getColumnIndex(ArtistHandler.BIO)));
			artist.setThumbnailURL(cursor.getString(cursor.getColumnIndex(ArtistHandler.IMAGE)));
		}
		cursor.close();
		return artist;
	}
	
	public static Artist getArtistByName(Context context, String name){
		Uri uri = Uri.withAppendedPath(CONTENT_URI, "" + name);
		Cursor cursor = context.getContentResolver().query(uri, null, null,
				null, null);
		Artist artist = new Artist();
		if (cursor.moveToNext()){
			artist.setId(cursor.getString(cursor.getColumnIndex(ArtistHandler.ID)));
			artist.setName(cursor.getString(cursor.getColumnIndex(ArtistHandler.NAME)));
			artist.setBio(cursor.getString(cursor.getColumnIndex(ArtistHandler.BIO)));
			artist.setThumbnailURL(cursor.getString(cursor.getColumnIndex(ArtistHandler.IMAGE)));
			cursor.close();
			return artist;
		}
		return null;
	}

	public static boolean delete(Context context, int id) {
		int rows = context.getContentResolver().delete(CONTENT_URI,
				"_id = " + id, null);
		if (rows != 0) {
			return true;
		}
		return false;
	}

}
